let deviceInfo

function getDeviceInfo() {
  if (deviceInfo) {
    return deviceInfo
  } else {
    let ua = window.navigator.userAgent
    let n = ua.toLowerCase()
    let detect = (p) => {
      return p.test(n)
    }
    deviceInfo = {}
    deviceInfo.isPhone = detect(/(iphone|ipod|((?:android)?.*?mobile)|j2me|mobi|blackberry|nokia|maemo|mini)/i)
    deviceInfo.isAndroid = detect(/android/i)
    deviceInfo.isIOS = detect(/(ipad|iphone|ipod)/i)
    deviceInfo.isWeiXin = detect(/micromessenger/i)
    deviceInfo.isApp = detect(/qixiu/i)
    if (deviceInfo.isIOS) {
      let ver = n.match(/ os (.*?) like mac os/)
      if (ver) {
        deviceInfo.IOSVersion = ver[1].replace(/_/g, '.')
      }
    }
    if (deviceInfo.isAndroid) {
      let reg = /android [\d._]+/gi
      let v_info = n.match(reg)
      deviceInfo.androidVersion = (v_info + '').replace(/[^0-9|_.]/gi, '').replace(/_/gi, '.')
    }
    //是不是小程序
    if (window.__wxjs_environment === 'miniprogram') {
      deviceInfo.isMP = true
    } else {
      deviceInfo.isMP = false
    }
    return deviceInfo
  }
}

Vue.prototype.$getDeviceInfo = getDeviceInfo

export default getDeviceInfo
